/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.persistence.dao.impl;

import edu.uoc.tdp.pac3.configuration.IConfiguration;
import edu.uoc.tdp.pac3.configuration.PropertiesConfiguration;
import edu.uoc.tdp.pac3.persistence.dao.IBaseDao;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorConnexioBD;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorSQL;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gerard
 */
abstract class BaseDao<S extends Serializable> extends GestorDisc implements IBaseDao<S> {
    
    private String tableName;
    
    private static IConfiguration configuration;
        
    private final String DELETE_STRING = "DELETE FROM " + tableName;
    
    private final String INSERT_STRING = "INSERT INTO " + tableName;
    
    private final String UPDADE_STRING = "UPDATE ";
    
    private final String SCHEMA;

    public BaseDao(IConfiguration configuration, String tableName) {
        this.configuration = configuration;
        this.SCHEMA = configuration.getProperty("schema");
        this.tableName = tableName;
        this.tableName = "\"" + SCHEMA + "\".\"" + tableName + "\"";  
    }
        
    public void openConnection() throws ErrorConnexioBD{
        super.obrirConnexio(configuration.getProperty(IConfiguration.DATABASE_URL),
                            configuration.getProperty(IConfiguration.DATABASE_USER),
                            configuration.getProperty(IConfiguration.DATABASE_PASSWD));
    }
    
    public abstract List<S> findAll();
       
    public abstract boolean insert(S entity);
    
    public abstract boolean update(S entity);
    
    public abstract boolean delete(S entity);
    
    public abstract S findById(Integer id);

    public Integer count(){
        Integer count = null;
        try {
            openConnection();
            count = super.count(tableName);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorSQL ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return count;
    }
    
    public Integer deleteAll(){
        Integer count = null;
        try {
            openConnection();
            count = super.esborrar(tableName);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorSQL ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return count;
    }

    public String getTableName() {
        return tableName;
    }

    public String getDELETE_STRING() {
        return DELETE_STRING;
    }

    public String getINSERT_STRING() {
        return INSERT_STRING;
    }

    public String getUPDADE_STRING() {
        return UPDADE_STRING;
    }

}
